﻿#region License
// (c) Garry McGlennon 2009
// 
// UnitTestEx is made available under the Microsoft Public License (Ms-PL)
// Code is provided as is and with no warrenty – Use at your own risk
// View the project and the latest code at http://codeplex.com/UnitTestEx/
// 
// All other rights reserved.
// 
// blog: www.dotNetProfessional.com/blog/
#endregion
using System;
using System.Collections.Generic;

namespace dnp.UnitTestEx.TestDoubles
{
    /// <summary>
    /// This class is used to provide service method monitoring support for unit testing. Each service method in your Fake service implementation should have a corresponding Spy property.
    /// As and example the method SaveCustomer would have the following property defined:
    /// 
    /// public VoidServiceMethodSpy SaveCustomerSpy {get;set;}
    /// </summary>
    public class VoidServiceMethodSpy
    {
        public bool WasCalled { get; set; }
        public int MethodCalledCount { get; set; }
        public Exception SimulateException { get; set; }
        public List<object[]> Parameters { get; set; }
        public event EventHandler OnMethodCalled;

        public VoidServiceMethodSpy()
        {
            this.Parameters = new List<object[]>();
        }

        public virtual void MethodCalled(params object[] parameters)
        {
            this.WasCalled = true;
            this.MethodCalledCount++;

            // Record parameters
            this.Parameters.Add(parameters);

            // Copy event handler to a temporary variable to be thread-safe.
            EventHandler eventHandler = OnMethodCalled;
            if (eventHandler != null)
            {
                eventHandler(this, EventArgs.Empty);
            }

            if (this.SimulateException != null)
                throw this.SimulateException;
        }
    }
}
